package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.snda.lib.util.FileHelper;
import com.snda.lib.util.TimeHelper;
import com.threeonemeeting.Const;
import com.threeonemeeting.R;
import com.threeonemeeting.model.DownloadInfo;
import com.threeonemeeting.util.MiscUtil;

public class DownloadItemListAdapter extends ArrayAdapter<DownloadInfo> {

	private ArrayList<DownloadInfo> downloadItems = null;

	public DownloadItemListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setDownloadList(ArrayList<DownloadInfo> downloadList) {
		downloadItems = downloadList;
		notifyDataSetChanged();
	}

	public ArrayList<DownloadInfo> getDownloadList() {
		return downloadItems;
	}

	@Override
	public void add(DownloadInfo downloadInfo) {
		if (downloadItems == null)
			downloadItems = new ArrayList<DownloadInfo>();

		downloadItems.add(downloadInfo);
	}

	public void clear() {
		if (downloadItems != null) {
			downloadItems.clear();
		}
	}

	@Override
	public int getCount() {
		if (downloadItems == null)
			return 0;

		return downloadItems.size();
	}

	@Override
	public DownloadInfo getItem(int position) {
		return downloadItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, final ViewGroup parent) {
		Activity activity = (Activity) getContext();

		DownloadInfo dInfo = getItem(position);
		if (dInfo == null) {
			Log.d(Const.Tag, "get item is null from list view:" + position);
			return convertView;
		}

		DownloadItemHolder holder = null;
		if (convertView == null) {
			LayoutInflater mInflater = activity.getLayoutInflater();
			convertView = mInflater.inflate(R.layout.lvi_download_item, null);
			holder = new DownloadItemHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (DownloadItemHolder) convertView.getTag();
		}

		LinearLayout processLayout = (LinearLayout) convertView
				.findViewById(R.id.layout_process);
		LinearLayout processBarLayout = (LinearLayout) convertView
				.findViewById(R.id.layout_process_bar);
		LinearLayout downTimeLayout = (LinearLayout) convertView
				.findViewById(R.id.layout_time);

		Bitmap bitmap = MiscUtil.getDocTypeBitmapByName(dInfo.strFileName);
		holder.iconIv.setImageBitmap(bitmap);

		ImageView statusImage = holder.statusIv;
		if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_RUNNING) {
			statusImage.setBackgroundResource(R.drawable.play);
		} else if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_CANCEL
				|| dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_FAILED) {
			statusImage.setBackgroundResource(R.drawable.pause);
		} else if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_FINISHED) {
			statusImage.setBackgroundResource(R.drawable.finish);
		}

		// 下载名称
		holder.nameTv.setText(dInfo.strFileTitle);

		// 正在下载
		if (dInfo.nStatus != DownloadInfo.DOWNLOAD_STATUS_FINISHED) {
			processLayout.setVisibility(View.VISIBLE);
			processBarLayout.setVisibility(View.VISIBLE);
			downTimeLayout.setVisibility(View.GONE);

			// 下载进度
			String strFileProcess = FileHelper
					.getSizeDesc(dInfo.nFileDownloadedSize)
					+ "/"
					+ FileHelper.getSizeDesc(dInfo.nFileSize);
			holder.processTv.setText(strFileProcess);

			// 下载百分比
			int nPercent = (int) (dInfo.nFileDownloadedSize * 100 / dInfo.nFileSize);
			holder.percentTv.setText(String.valueOf(nPercent) + "%");

			// 进度条
			int nCurrentPercent = holder.processBar.getProgress();
			if (nCurrentPercent != nPercent) {
				holder.processBar.setProgress(nPercent);
			}

		} else {
			processLayout.setVisibility(View.GONE);
			processBarLayout.setVisibility(View.GONE);
			downTimeLayout.setVisibility(View.VISIBLE);

			String strTime = TimeHelper.dateToString(
					TimeHelper.intToDate(dInfo.nDownloadTime),
					"yyyy年MM月dd日 hh点");
			holder.timeTv.setText(strTime);
		}

		return convertView;
	}

	private class DownloadItemHolder {

		ImageView iconIv;
		ImageView statusIv;
		TextView nameTv;
		TextView processTv;
		TextView percentTv;
		TextView timeTv;
		ProgressBar processBar;

		DownloadItemHolder(View baseView) {

			iconIv = (ImageView) baseView.findViewById(R.id.app_icon);

			statusIv = (ImageView) baseView.findViewById(R.id.app_status);
			nameTv = (TextView) baseView.findViewById(R.id.app_name);
			processTv = (TextView) baseView
					.findViewById(R.id.app_download_process);
			percentTv = (TextView) baseView
					.findViewById(R.id.app_download_percent);
			processBar = (ProgressBar) baseView
					.findViewById(R.id.app_processbar);
			timeTv = (TextView) baseView.findViewById(R.id.app_download_time);
		}
	}

}